package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ponhu.ea2.stockcenter.dto.StockBatchChangeQueryDTO;
import com.ponhu.ea2.stockcenter.dto.WMSEventInfoDTO;
import com.ponhu.ea2.stockcenter.dto.WMSInventoryInfo;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryBatchChange;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryBatchRecord;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseChange;
import com.ponhu.ea2.stockcenter.provider.entity.StockWarehouseChangeSettings;
import com.ponhu.ea2.stockcenter.provider.mapper.StockInventoryBatchChangeMapper;
import com.ponhu.ea2.stockcenter.provider.service.biz.CalcStockDataService;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryBatchChangeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.provider.support.exception.StockRuntimeException;
import com.ponhu.ea2.stockcenter.provider.utils.ProBeanUtils;
import com.ponhu.ea2.stockcenter.vo.StockBatchChangeVO;
import io.netty.util.concurrent.FastThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.ponhu.ea2.stockcenter.enums.ResponseCodeEnum.STOCK_WAREHOUSE_EXCEPTION_900116;

@Service
public class StockInventoryBatchChangeServiceImpl
        extends ServiceImpl<StockInventoryBatchChangeMapper, StockInventoryBatchChange>
        implements StockInventoryBatchChangeService {

    @Autowired
    private StockInventoryBatchChangeMapper stockInventoryBatchChangeMapper;

    @Autowired // 库存变动动作配置信息
    private FastThreadLocal<StockWarehouseChangeSettings> settingsLocal;

    @Override
    public List<StockInventoryBatchRecord> saveBatchChanges(List<StockWarehouseChange> changeList,
                                                            WMSEventInfoDTO eventInfoDTO, List<StockInventoryBatchRecord> batchRecordList) {

        Map<String, String> batchCodeMap = eventInfoDTO.getInventoryInfoList().stream()
                .filter(info -> StringUtils.isNotEmpty(info.getBatchCode())).collect(Collectors
                        .toMap(WMSInventoryInfo::getUniqueCode, WMSInventoryInfo::getBatchCode));

        Map<String, StockInventoryBatchRecord> batchRecordMap = batchRecordList.parallelStream()
                .collect(Collectors.toMap(StockInventoryBatchRecord::getInventoryCodeAndBatchCode, Function.identity()));

        StockWarehouseChangeSettings settings = settingsLocal.get();

        CopyOnWriteArrayList<StockInventoryBatchRecord> copyOnWriteArrayList = new CopyOnWriteArrayList();

        List<StockInventoryBatchChange> batchChangeList = changeList.parallelStream()
                .map(stockChange -> {
                    String batchCode = batchCodeMap.get(stockChange.getUniqueCode());
                    StockInventoryBatchChange batchChange = ProBeanUtils
                            .copyProperties(stockChange, StockInventoryBatchChange.class)
                            .setOperatorName(eventInfoDTO.getOperatorName())
                            .setOperatorTime(eventInfoDTO.getOperatorTime())
                            .setChangeId(stockChange.getId())
                            .setBatchCode(batchCode);

                    StockInventoryBatchRecord batchRecord = new StockInventoryBatchRecord()
                            .setInventoryCode(stockChange.getInventoryCode())
                            .setInventoryId(stockChange.getInventoryId())
                            .setBatchCode(batchCode)
                            .setBatchNum(0);
                    batchRecord = batchRecordMap.getOrDefault(batchChange.getInventoryCodeAndBatchCode(), batchRecord);
                    Integer batchNum = batchRecord.getBatchNum();
                    Integer postNum = CalcStockDataService
                            .getOperatorResult(batchNum, batchChange.getUpdatedNum(), settings.getRealStockAction());

                    copyOnWriteArrayList.add(batchRecord.setBatchNum(postNum));

                    return batchChange.setPreStockNum(batchNum).setPostStockNum(postNum);
                })
                .filter(batchChange -> StringUtils.isNotEmpty(batchChange.getBatchCode()))
                .collect(Collectors.toList());

        if (!saveOrUpdateBatch(batchChangeList))
            throw new StockRuntimeException(STOCK_WAREHOUSE_EXCEPTION_900116);

        return copyOnWriteArrayList;
    }

    @Override
    public IPage<StockBatchChangeVO> getStockBatchChangeListByQuery(StockBatchChangeQueryDTO queryDTO) {

        Page queryPageInfo = new Page(queryDTO.getPageIndex(), queryDTO.getPageSize());
        queryPageInfo.setCountId("getStockBatchChangeListByQuery_Count");
        return stockInventoryBatchChangeMapper.getStockBatchChangeListByQuery(queryDTO, queryPageInfo);
    }

}
